package com.exam.springbootexam.controller;
import com.exam.springbootexam.entity.Class;
import com.exam.springbootexam.entity.PageEntity;
import com.exam.springbootexam.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author 十五
 */
@RestController
@RequestMapping("/class")
@CrossOrigin
public class ClassController {
    @Autowired
    private ClassService classService;

    /**
     * 分页查询班级信息
     *
     * @param current  当前页码
     * @param pageSize 每页显示数量
     * @return 包含分页班级信息的响应实体
     */
    @GetMapping("/{current}/{pageSize}")
    public ResponseEntity<PageEntity<Class>> page(@PathVariable(value = "current") Integer current, @PathVariable(value = "pageSize") Integer pageSize) {
        PageEntity<Class> clazzPageEntity = classService.selectByPage(current, pageSize);
        return ResponseEntity.ok(clazzPageEntity);
    }

    /**
     * 根据班级ID查询班级信息
     *
     * @param id 班级ID
     * @return 查询到的班级信息或错误响应实体
     */
    @GetMapping("/{id}")
    public ResponseEntity<Class> findById(@PathVariable(value = "id") Integer id) {
        Optional<Class> clazz = Optional.ofNullable(classService.selectById(id));
        return clazz.map(ResponseEntity::ok).orElse(ResponseEntity.badRequest().build());
    }

    /**
     * 查询所有班级信息
     *
     * @return 所有班级信息列表的响应实体
     */
    @GetMapping("/all")
    public ResponseEntity<List<Class>> findAll() {
        List<Class> clazzs = classService.selectAll();
        return ResponseEntity.ok(clazzs);
    }

    /**
     * search 搜索
     */
    @GetMapping("/search")
    public ResponseEntity<List<Class>> search(@RequestParam String keyword) {
        List<Class> clazzs = classService.serachByKeyword(keyword);
        return ResponseEntity.ok(clazzs);
    }

    /**
     * 修改班级信息
     *
     * @param clazz 班级对象
     * @return 更新结果的响应实体
     */
    @PostMapping("/modify")
    public ResponseEntity<?> update(@RequestBody Class clazz) {
        boolean rs = classService.update(clazz);
        if (rs) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 删除班级
     *
     * @param clazz 班级ID
     * @return 删除结果的响应实体
     */
    @PostMapping("/delete")
    public ResponseEntity<?> delete(@RequestBody Class clazz) {
        boolean rs = classService.delete(clazz.getClassId());
        if (rs) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 班级添加
     *
     * @param clazz 班级对象
     * @return 添加结果的响应实体
     */
    @PostMapping("/add")
    public ResponseEntity<?> register(@RequestBody Class clazz) {
        if (classService.selectById(clazz.getClassId()) != null) {
            return ResponseEntity.badRequest().build();
        }
        boolean rs = classService.add(clazz);
        if (rs) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.badRequest().build();
        }
    }
}
